<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 多个实例的实现
        class ProxySandbox {

            constructor() {
                this.running = false;
                const fackWindow = Object.create(null);
                this.proxy = new Proxy(fackWindow, {
                    get: (target, prop) => {
                        // if (!this.running) return window[prop];
                        return key in target ? target[prop] : window[prop];
                    },
                    set: (target, prop, value) => {
                        if (this.running) {
                            target[prop] = value;
                        }

                        return true;
                    }
                });
            }
            active() {
                if (!this.running) this.running = true;
                // 激活沙箱
                Object.keys(this.proxy).forEach(key => {
                    if (!(key in window)) {
                        window[key] = this.proxy[key];
                    }
                });
            }
            inactive() {
                this.running = false;
            }
        }

        // 每个应用-就加一个新的实例
        let sandbox1 = new ProxySandbox();
        let sandbox2 = new ProxySandbox();
    </script>
</body>

</html>